package com.bytedance.douyinclouddemo.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

public class TestCode {
    public static void main(String[] args) throws UnsupportedEncodingException {
//        String url="https://p3-developer-sign.bytemaimg.com/tos-cn-i-ke512zj2cu/9b0abdfc8b9a4cc28b99430c3efe62f1~tplv-noop.jpeg?rk3s=3839646du0026x-expires=1751851776u0026x-signature=VNAfIpJhVZexNwJSITnYrcYTE9c%3D";
//        System.out.println(convertUnicodeEscapeSequences(url));
//        //System.out.println(String.format("%s",url));
//        String url2="[{\"msg_id\":\"7521551329186092042\",\"sec_openid\":\"7520869510808802358\",\"avatar_url\":\"https://p3-developer-sign.bytemaimg.com/tos-cn-i-ke512zj2cu/9b0abdfc8b9a4cc28b99430c3efe62f1~tplv-noop.jpeg?rk3s=3839646du0026x-expires=1751851776u0026x-signature=VNAfIpJhVZexNwJSITnYrcYTE9c%3D\",\"nickname\":\"智汇中天测试1\",\"fansclub_reason_type\":2,\"fansclub_level\":1,\"timestamp\":1751247697135}]";
//        System.out.println(convertUnicodeEscapeSequences(url2));
        String content="[{\"msg_id\":\"7521574474949563411\",\"sec_openid\":\"7520869510808802358\",\"content\":\"good高兴boy!\",\"avatar_url\":\"https://p3-developer-sign.bytemaimg.com/tos-cn-i-ke512zj2cu/9b0abdfc8b9a4cc28b99430c3efe62f1~tplv-noop.jpeg?rk3s=3839646d&x-expires=1751851776&x-signature=VNAfIpJhVZexNwJSITnYrcYTE9c%3D\",\"nickname\":\"智汇中天测试1\",\"timestamp\":1751253068025}]";
        String like="[{\"msg_id\":\"7521574897521923135\",\"sec_openid\":\"7520869510808802358\",\"like_num\":10,\"avatar_url\":\"https://p3-developer-sign.bytemaimg.com/tos-cn-i-ke512zj2cu/9b0abdfc8b9a4cc28b99430c3efe62f1~tplv-noop.jpeg?rk3s=3839646d&x-expires=1751851776&x-signature=VNAfIpJhVZexNwJSITnYrcYTE9c%3D\",\"nickname\":\"智汇中天测试1\",\"timestamp\":1751253142621}]";
        System.out.println(like);
//        String url3=url2;
//        System.out.println(String.format("%s",url3));
//        String unicodeStr = "u4F60u597Du4E16u754C"; // 注意这里的双反斜杠，因为在字符串中表示一个反斜杠需要使用两个反斜杠
//        //String s = unicodeStr.replaceAll("u", "u");
//        //String formattedStr = String.format("%s", unicodeStr); // 或者使用 String formattedStr = new String(unicodeStr);
//        System.out.println(convertUnicodeEscapeSequences(unicodeStr)); // 输出: 你好世界


    }

    public static String convertUnicodeEscapeSequences(String input) {
        StringBuilder result = new StringBuilder();
        int i = 0;
        while (i < input.length()) {
            if (input.startsWith("u", i)) {
                // 提取Unicode转义序列
                String unicodeSequence = input.substring(i, i + 6);
                try {
                    // 将转义序列转换为对应的字符
                    char unicodeChar = (char) Integer.parseInt(unicodeSequence.substring(2), 16);
                    result.append(unicodeChar);
                    // 移动索引到转义序列之后
                    i += 6;
                } catch (NumberFormatException e) {
                    // 如果转义序列无效，则原样保留
                    result.append(unicodeSequence);
                    i += 2; // 至少跳过`u`
                }
            } else {
                // 如果不是转义序列，则原样保留字符
                result.append(input.charAt(i));
                i++;
            }
        }
        return result.toString();
    }
}
